Public Class Email


    Public ToAddress As String
    Public FromAddress As String
    Public SMTPServer As String = "COASTMAIL.cam.com"
    Public Body As String
    Public Subject As String
    Public Attachment As System.Net.Mail.Attachment
    Private WithEvents t As System.Timers.Timer = New System.Timers.Timer
    Private email As New System.Net.Mail.MailMessage
    Private iTryCount As Integer = 1
    Public oLog As SysLog


    Public Function Send() As Boolean
        Try
            email.To.Add(New System.Net.Mail.MailAddress(ToAddress))
            email.From = New System.Net.Mail.MailAddress(FromAddress)
            email.Body = Body
            email.Subject = Subject
            email.IsBodyHtml = False
            Dim smtp As New System.Net.Mail.SmtpClient()
            smtp.Host = SMTPServer
            Try
                smtp.Send(email)
                Return True
            Catch ex As Exception
                If Not oLog Is Nothing Then
                    oLog.LogText("Try 1 of sending mail failed." + ex.Message)
                End If
                RetryEmail()
                Throw ex
            End Try
        Catch ex As Exception
            If Not oLog Is Nothing Then
                oLog.LogText("Try 1 of sending mail failed." + ex.Message)
            End If
            Throw ex
            Return False
        End Try
    End Function

    Public Function SendWithAttachment() As Boolean
        Try
            email.To.Add(New System.Net.Mail.MailAddress(ToAddress))
            email.From = New System.Net.Mail.MailAddress(FromAddress)
            email.Body = Body
            email.Subject = Subject
            email.IsBodyHtml = False
            email.Attachments.Add(Attachment)
            Dim smtp As New System.Net.Mail.SmtpClient()
            smtp.Host = SMTPServer
            Try
                smtp.Send(email)
                Return True
            Catch ex As Exception
                oLog.LogText("Try 1 of sending mail failed." + ex.Message)
                RetryEmail()
                Throw ex
            End Try
        Catch ex As Exception
            oLog.LogText("Try 1 of sending mail failed." + ex.Message)
            Throw ex
            Return False
        End Try
    End Function

    Public Function RetryEmail() As Boolean
        'Starts a timer which retries every 30 seconds until the message is sent.
        iTryCount = 1
        t.Interval = 30000      '30 seconds.
        t.AutoReset = True
        t.Start()
    End Function

    Private Function ReSendEmail() As Boolean
        Try
            iTryCount = iTryCount + 1
            Dim smtp As New System.Net.Mail.SmtpClient()
            smtp.Host = SMTPServer
            smtp.Send(email)
            Return True
        Catch ex As Exception
            oLog.LogText("Try " + iTryCount.ToString + " of sending mail failed." + ex.Message)
            Return False
        End Try
    End Function

    Private Sub t_Tick(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles t.Elapsed
        If iTryCount < 5 Then
            If ReSendEmail() Then
                t.Stop()
            End If
        Else
            oLog.LogText("Maximum number of retries for email reached.")
            t.Stop()
        End If
    End Sub

End Class
